package com.system.borrow.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.system.borrow.MyApplication;
import com.system.borrow.R;
import com.system.borrow.bean.SearchTag;
import com.system.borrow.fragment.SearchFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/11/30 19:38
 * 描    述：TODO
 * ================================================
 */
public class GalleryAdapter extends
        RecyclerView.Adapter<GalleryAdapter.ViewHolder>
{
    private LayoutInflater mInflater;
    private List<SearchTag> list = new ArrayList<>();
    public GalleryAdapter(Context context)
    {
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<SearchTag> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }

        ImageView rl_bg;
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.item_recycler,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.rl_bg = (ImageView) view.findViewById(R.id.rl_bg);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) viewHolder.rl_bg.getLayoutParams();
        if(i==0){
            params.setMargins(50,0,16,0);

        }else{
            params.setMargins(0,0,16,0);
        }
        viewHolder.rl_bg.setLayoutParams(params);
        final SearchTag searchTag = list.get(i);
        MyApplication.imageLoader.displayImage(searchTag.img,viewHolder.rl_bg);
        viewHolder.rl_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchFragment.searchFragment.loadData(searchTag.id);
            }
        });
    }
}
